const fs = require('fs');
var liaotian = function (objs, wxshowobj, root) {//聊天消息转发
	// console.log(root)
	objs.sendtime = new Date().getTime();
	let sendid = objs.sendid;
	// console.log(sendid)
	if (!wxshowobj[sendid + 'wx']) {
		let jsond = require(root + '/olddata/' + sendid + '.json');
		jsond.push(objs);
		fs.unlinkSync(root + '/olddata/' + sendid + '.json');
		fs.writeFile(root + '/olddata/' + sendid + '.json', JSON.stringify(jsond), { 'flag': 'a' }, function (err) {
			if (err) {
				throw err;
			}
		});
	} else {
		wxshowobj[sendid + 'wx'].websocket.send(JSON.stringify([objs]));
	};
}
var yanzheng = function (objs, wxshowobj, root) {//验证消息转发
	objs.sendtime = new Date().getTime();
	let sendid = objs.sendid;
	console.log(objs)
	if (!wxshowobj[sendid + 'wx']) {
		let jsond = require(root + '/yanzheng/' + sendid + '.json');
		jsond.push(objs);
		fs.unlinkSync(root + '/yanzheng/' + sendid + '.json');
		fs.writeFile(root + '/yanzheng/' + sendid + '.json', JSON.stringify(jsond), { 'flag': 'a' }, function (err) {
			if (err) {
				throw err;
			}
		});
	} else {
		wxshowobj[sendid + 'wx'].websocket.send(JSON.stringify([objs]));
	};
}

var flist = function (objs, wxshowobj, root) {//朋友圈评论信息转发
	objs.sendtime = new Date().getTime();
	let sendid = objs.sendid;

	if (!wxshowobj[sendid + 'wx']) {//用户离线状态 写入离线消息文件夹
		let jsond = require('../flist/' + sendid + '.json');
		jsond.push(objs);
		fs.unlinkSync(root + '/flist/' + sendid + '.json');
		fs.writeFile(root + '/flist/' + sendid + '.json', JSON.stringify(jsond), { 'flag': 'a' }, function (err) {
			if (err) {
				throw err;
			}
		});
	} else {
		wxshowobj[sendid + 'wx'].websocket.send(JSON.stringify([objs]));//用户在线直接转发信息
	};
};

function index(app, websockrou, wxshowobj) {
	app.ws.use(websockrou.all('/home', function (ctx) {
		var root = ctx.root;
		let uid = ctx.request.query.uid;

		wxshowobj[uid + 'wx'] = ctx;//用户上线 以uid参数保存socket对象
		let newd = require(root + '/olddata/' + uid + '.json');
		// console.log(newd,root+'/olddata/' + uid + '.json')
		let newds = require(root + '/yanzheng/' + uid + '.json');
		let newdss = require(root + '/flist/' + uid + '.json');
		// console.log(uid,root+'/yanzheng/' + uid + '.json')
		newds.yanzheng = true;
		newdss.flist = true;
		// console.log(newd)//用户上线获取一次离线消息然后清空离线消息
		ctx.websocket.send(JSON.stringify(newd));//离线聊天消息
		ctx.websocket.send(JSON.stringify(newds));//离线验证消息
		ctx.websocket.send(JSON.stringify(newdss));//离线朋友圈评论消息

		if (newd.length) {//清空离线聊天消息
			fs.unlinkSync(root + '/olddata/' + uid + '.json');
			fs.writeFile(root + '/olddata/' + uid + '.json', JSON.stringify([]), { 'flag': 'a' }, function (err) {
				if (err) {
					throw err;
				}
			});
		};
		if (newds.length) {//清空离线验证消息
			fs.unlinkSync(root + '/yanzheng/' + uid + '.json');
			fs.writeFile(root + '/yanzheng/' + uid + '.json', JSON.stringify([]), { 'flag': 'a' }, function (err) {
				if (err) {
					throw err;
				}
			});
		};
		if (newdss.length) {//清空离线朋友圈评论消息
			fs.unlinkSync(root + '/flist/' + uid + '.json');
			fs.writeFile(root + '/flist/' + uid + '.json', JSON.stringify([]), { 'flag': 'a' }, function (err) {
				if (err) {
					throw err;
				}
			});
		};

		ctx.websocket.on('message', function (message) {
			let objs = JSON.parse(message);
			console.log(objs)
			if (objs.yanzheng) {//验证消息
				yanzheng(objs, wxshowobj, root)
			} else {
				if (objs.flist) {//朋友圈评论消息
					flist(objs, wxshowobj, root);
					return;
				};
				if (objs.reset) {//朋友圈评论消息
					let sendid = objs.sendid;
					if (wxshowobj[sendid + 'wx']) {//申请好友添加反馈
						wxshowobj[sendid + 'wx'].websocket.send(JSON.stringify([objs]));
					}
					return;
				};
				liaotian(objs, wxshowobj, root);//聊天消息
			}
		});

		ctx.websocket.on("close", (message) => {//检测离线 //用户离线 删除链接
			delete wxshowobj[uid + 'wx'];
		});
	}))
}
module.exports = index;